package leetcode.list;

import java.util.HashSet;

/**
 * @author mazouri
 * @create 2021-08-18 23:31
 */
public class _160_getIntersectionNode_hash {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        HashSet<ListNode> set = new HashSet<>();
        if (headA == null || headB == null) return null;

        while (headA != null) {
            set.add(headA);
            headA = headA.next;
        }

        while (headB != null) {
            if (set.contains(headB)){
                return headB;
            }
            headB = headB.next;
        }

        return null;
    }
}
